package com.winchain.chainbill.bill.biz.receive.cmd;

import com.shell.bee.base.utils.DateUtil;
import com.shell.framework.command.CommandContext;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.SysRcvAutoRejectKeyword;
import com.shell.governor.modules.message.keyword.SysRcvRejectKeyword;
import com.shell.governor.modules.message.keyword.SysXfeRcvRefuseKeyword;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.send.cmd.TxCmd;
import org.springframework.stereotype.Component;

@Component
public class BillRcvRjctMsgCmd extends TxCmd<CbRcv> {
	public boolean execute(CommandContext<Bill<CbRcv>> context) throws Exception {
		CbRec cbRec = ((Bill) context.getSource()).getRec();
		CbRcv cbRcv = (CbRcv) ((Bill) context.getSource()).getSource();
		CbOpt opt = (CbOpt) context.getValue("Operation");
		String txType = cbRcv.getTxType();
		boolean exp = false;
		Object expObj = context.getValue("exp");
		if (expObj != null) {
			exp = ((Boolean) expObj).booleanValue();
		}
		if (exp) {
			SysRcvAutoRejectKeyword sysRcvAutoRejectKeyword = new SysRcvAutoRejectKeyword();
			sysRcvAutoRejectKeyword.setBuyerNm(cbRcv.getBuyerNm());
			sysRcvAutoRejectKeyword.setSellerNm(cbRcv.getSellerNm());
			sysRcvAutoRejectKeyword
					.setPayDate(DateUtil.parseDate(cbRcv.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
			sysRcvAutoRejectKeyword.setBillAmt(String.valueOf(cbRec.getBillAmt()));

			String template = "MT02205";
			MessageSender.sendMessageByTmpl(CommonDataUtil.getUserId(), template, sysRcvAutoRejectKeyword,
					cbRcv.getBuyerId());

			template = "MT02206";
			MessageSender.sendMessageByTmpl(CommonDataUtil.getUserId(), template, sysRcvAutoRejectKeyword,
					cbRcv.getSellerId());
		} else if ("0".equals(txType)) {
			String template = "MT01307";
			SysRcvRejectKeyword keyWord = new SysRcvRejectKeyword();
			keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));
			keyWord.setSellerNm(cbRcv.getSellerNm());
			keyWord.setSndDate(DateUtil.parseDate(cbRcv.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
			keyWord.setUserName(CommonDataUtil.getUserName());
			keyWord.setRemark(opt.getRemark());
			String senderId = CommonDataUtil.getUserId();
			String buyerId = cbRcv.getBuyerId();
			MessageSender.sendMessageByTmpl(senderId, template, keyWord, buyerId);

			template = "MT02207";

			MessageSender.sendMessageByTmpl(senderId, template, keyWord, buyerId);
		} else if ("1".equals(txType)) {
			String template = "MT01406";
			SysXfeRcvRefuseKeyword keyWord = new SysXfeRcvRefuseKeyword();
			keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));
			keyWord.setSellerNm(cbRcv.getSellerNm());
			keyWord.setSndDate(DateUtil.parseDate(cbRcv.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
			keyWord.setUserName(CommonDataUtil.getUserName());
			keyWord.setRemark(opt.getRemark());
			String senderId = CommonDataUtil.getUserId();
			String buyerId = cbRcv.getBuyerId();
			MessageSender.sendMessageByTmpl(senderId, template, keyWord, buyerId);

			template = "MT02208";
			MessageSender.sendMessageByTmpl(senderId, template, keyWord, buyerId);
		}
		return false;
	}
}
